package cn.micai.base.concurrent;

/**
 * 描述：使用 CAS 替代锁定
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/5/14 10:46
 */
public class CasCounter {

    private SimulatedCAS value;

    public int getValue() {
        return value.getValue();
    }

    public int increment() {
        int oldValue = value.getValue();
        while (value.compareAndSwap(oldValue, oldValue + 1) != oldValue) {
            oldValue = value.getValue();
        }
        return oldValue + 1;
    }
}
